#include "../tools.h"

// 二分查找
// 你可以假设 nums[-1] = nums[n] = -∞ 这句是重点，可以由此推断出下一步的区间
class Solution {
public:
    int bsearch(vector<int>& nums){
        int size = nums.size();
        int left = 0, right = nums.size();
        while(left < right){
            int mid = left + (right - left) / 2;
            if((mid <= 0 || nums[mid - 1] < nums[mid]) && (mid >= size - 1 || nums[mid] > nums[mid + 1])) return mid;
            else if(nums[mid] <= nums[mid - 1]) right = mid;
            else if(nums[mid] <= nums[mid + 1]) left = mid + 1;
        }
        return left;
    }
    int findPeakElement(vector<int>& nums) {
        return bsearch(nums);
    }
};

int main(int argc, char const *argv[])
{
    Solution sol;

    vector<int> nums = {1,2,3};

    cout << sol.findPeakElement(nums) << endl;

    return 0;
}
